(function () {
    var dom = ecui.dom,
        now = new Date(),
        form = 'sponsorAddCompanyForm';
    NS.ui.SaveOrder = ecui.inherits(
        mao.ui.Submit,
        function (el, options) {
            mao.ui.Submit.call(this, el, options);
            this._sOptId = options.optId;
        },
        {
            $click: function () {
                var file = document.forms[form].elements.file;
                file.name = '';
                this.request('data@FORM /erp-management/customer/sponsor/' + (this._sOptId ? 'update' : 'add') + '?sponsorAddForm&' + form, function () {
                    file.name = ecui.dom.getAttribute(file, 'data-name');
                    var data = ecui.esr.getData('data');
                    if (data instanceof Object) {
                        if (this._sOptId) {
                            ecui.tip('warn', '主办方编辑成功');
                            delete ecui.esr.getRoute('/approval/initiate.initiate').CACHE;
                            ecui.esr.redirect('/approval/initiate.initiate');
                        } else {
                            ecui.tip('warn', '主办方新增成功');
                            history.go(-1);
                        }
                    }
                }, function () {
                    file.name = ecui.dom.getAttribute(file, 'data-name');
                    ecui.tip('warn', '请完成所有必填项的输入');
                });
            }
        }
    );
    NS.ui.Type = ecui.inherits(
        ecui.ui.Radio,
        {
            onclick: function (event) {
                var value = +this.getValue();
                form = value === 1 ? 'sponsorAddCompanyForm' : 'sponsorAddPeopleForm';
                dom[value === 1 ? 'removeClass' : 'addClass'](document.forms.sponsorAddCompanyForm, 'ui-hide');
                dom[value === 1 ? 'addClass' : 'removeClass'](document.forms.sponsorAddPeopleForm, 'ui-hide');
            }
        }
    );
    ecui.esr.addRoute('edit', {
        model: function (context, callback) {
            var model = [
                'detail@GET /erp-management/customer/sponsor/${id}'
            ];
            if (!context.id) {
                model.splice(0, 1);
            }
            ecui.esr.request(model, callback);
            return false;
        },

        weight: 10,
        frame: true,
        notleave: true,
        onbeforerender: function (context) {
            ecui.esr.setData('sponsorEditTitle', context.id ? '主办方编辑' : '主办方新增');
            mao.util.setTitle(context.id ? '主办方编辑' : '主办方新增');
            this.notleave = true;

            var detail = context.detail || {};
            form = detail.type === 1 ? 'sponsorAddCompanyForm' : 'sponsorAddPeopleForm';
        },
        oncached: function () {
            this.notleave = true;
        },
        onleave: function (context, callback) {
            if (this.notleave && ecui.util.checkUpdate(document.forms[form])) {
                ecui.$messagebox(
                    'ui-messagebox-confirm',
                    '是否放弃正在编辑的信息？',
                    ['继续编辑', '放弃编辑'],
                    function () {
                        callback(false);
                    },
                    function () {
                        callback(true);
                    }
                );
                return false;
            }
        }
    });
}());